package com.gitee.feizns.quickstart.mybatis.flex.query;

import com.gitee.feizns.quickstart.domain.page.PageAo;
import com.gitee.feizns.quickstart.domain.page.PageRecordsVo;
import com.gitee.feizns.quickstart.domain.page.PageVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.PageObjectUtil;

import java.util.List;
import java.util.function.Supplier;

/**
 * @author feizns
 * @since 2025/3/5
 */
public class Page<T> extends com.mybatisflex.core.paginate.Page<T> {

    /**
     * 分页参数
     * @param current 当前
     * @param size 大小
     */
    public Page(Number current, Number size) {
        super(current, size);
    }

    /**
     * 之
     * @param current 当前
     * @param size 大小
     * @return {@link Page}<{@link T}>
     */
    public static <T> Page<T> of(Number current, Number size) {
        return new Page<>(current, size);
    }

    /**
     * 之
     * @param page 分页参数
     * @return {@link Page}<{@link T}>
     */
    public static <T> Page<T> of(PageAo page) {
        return new Page<>(page.getCurrent(), page.getSize());
    }

    /**
     * 自分页参数记录响应对象
     * @return {@link PageRecordsVo}<{@link T}>
     */
    public PageRecordsVo<T> toPageRecordsVo() {
        return new MybatisFlexPageRecordsVoImpl<>(this);
    }

    /**
     * 自分页参数记录响应参数
     * @param listSupplier 列出供应商
     * @return {@link PageRecordsVo }<{@link T }>
     */
    public PageRecordsVo<T> toPageRecordsVo(Supplier<List<T>> listSupplier) {
        try ( com.github.pagehelper.Page<Object> page = PageHelper.startPage((int) this.getPageNumber(), (int) this.getPageSize()) ) {
            List<T> es = listSupplier.get();
            return new PageInfoPageRecordsVoImpl<>(new PageInfo<>(es));
        }
    }

}
